package activities;

import java.util.concurrent.ExecutionException;

import model.ProductItem;
import pkg.inno.fileshare.R;
import Tasks.GetProductItemAsync;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ProductsView extends Activity implements OnItemClickListener
{
	ListView _listViewObject;
	ArrayAdapter<ProductItem> _adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.products_view);
		
		_listViewObject=(ListView)findViewById(R.id.listViewProducts);
		
		GetProductItemAsync retriever =new GetProductItemAsync();
		try {
			_adapter=retriever.execute(this).get();
			_listViewObject.setAdapter(_adapter);
			
			//_listViewObject.setOnClickListener(this);
			_listViewObject.setOnItemClickListener(this);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
	{
		ProductItem productObj=_adapter.getItem(arg2);
		
		Bundle bundleObj=new Bundle();
		bundleObj.putString("productId", productObj.get_productItemId());
		bundleObj.putString("productName", productObj.get_productName());
		
		Intent intentObj=new Intent(ProductsView.this,ProductView.class);
		intentObj.putExtra("productBundle", bundleObj);
		
		startActivity(intentObj);
		finish();
	}

}
